package com.sanpu.kxe.business.card.presenter;

import android.app.Activity;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.sanpu.kxe.business.card.bean.BankInfoInput;
import com.sanpu.kxe.business.card.bean.CardOutput;
import com.sanpu.kxe.business.card.contract.AddBankSetupContract;
import com.sanpu.kxe.business.card.model.CardModel;
import com.sanpu.kxe.business.card.provider.CardOpenProvider;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.module.dialog.CertificateDialog;
import com.sanpu.kxe.utils.PublicProviderUtil;
import com.sanpu.kxe.utils.ToastUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：添加银行卡第一步
 * Created by 薛勇军 on 2018/7/24.
 */

public class AddBankSetupPresenter implements AddBankSetupContract.Presenter {

    private AddBankSetupContract.View mView;

    private String chkSts;

    private String crpName;

    private CompositeSubscription mSubscription;

    public AddBankSetupPresenter(AddBankSetupContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void initViewData() {
        UserCenterOutput mOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (mOutput != null) {
            chkSts = mOutput.getChkSts();
            if (!TextUtils.equals(CommonConfig.AUTHEN_PASS, chkSts)) {
                new CertificateDialog(mView.getContext(), new DialogViewListener() {
                    @Override
                    public void btnRightConfirm() {
                        if (!TextUtils.equals(CommonConfig.AUTHEN_AUDIT, chkSts)) {
                            openCertificationActivity();
                        } else {
                            ((Activity)mView.getContext()).finish();
                        }
                    }

                    @Override
                    public void btnLeftCancel() {
                        ((Activity)mView.getContext()).finish();
                    }
                }).show();
                return;
            }
            if (!TextUtils.isEmpty(mOutput.getCrpNm())) {
                crpName = mOutput.getCrpNm();
                mView.showCrpName(crpName);
            }
        }
    }

    @Override
    public void onNextClick(final String bankNumber, final String billDay, final String dueDay) {
        if (TextUtils.isEmpty(bankNumber)) {
            ToastUtil.showTextViewPrompt("请先填写你的银行卡号");
            return;
        }
        if (TextUtils.isEmpty(billDay)) {
            ToastUtil.showTextViewPrompt("请选择账单日");
            return;
        }
        if (TextUtils.isEmpty(dueDay)) {
            ToastUtil.showTextViewPrompt("请选择还款日");
            return;
        }
        if (TextUtils.equals(billDay, dueDay)) {
            ToastUtil.showTextViewPrompt("信用卡账单日与还款日不能设为同一天");
            return;
        }

        mView.showLoadingDialog(false);
        Subscription subscription = CardModel.getInstance().bankInfo(bankNumber, false, true)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<CardOutput>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                    }

                    @Override
                    public void onNext(CardOutput output) {
                        if (output != null) {
                            output.setBankNumber(bankNumber);
                            if (TextUtils.isEmpty(output.getCardType())) {
                                ToastUtil.showTextViewPrompt("暂时不支持该卡");
                                return;
                            }
                            if (!CommonConfig.DEPOSIT_CARD_TYPE.equals(output.getCardType())
                                    && !CommonConfig.CREDIT_CARD_TYPE.equals(output.getCardType())) {
                                ToastUtil.showTextViewPrompt("暂时不支持该卡");
                                return;
                            }
                            openAddBankSecondActivity(output, billDay, dueDay);
                        }
                    }
                });
        mSubscription.add(subscription);
    }

    /**
     * 添加银行卡第二步
     */
    private void openAddBankSecondActivity(@NonNull CardOutput output, String billDay, String dueDay) {
        BankInfoInput infoInput = new BankInfoInput();
        infoInput.setCrpNm(crpName);
        infoInput.setBankNo(output.getBankNumber());
        infoInput.setBankCode(output.getBankCode());
        infoInput.setCardType(output.getCardType());
        infoInput.setBankName(output.getBankName());
        infoInput.setBillDay(billDay.substring(2, billDay.length()-1));
        infoInput.setDueDay(dueDay.substring(2, dueDay.length()-1));
        CardOpenProvider.getInstance().openAddBankSecondActivity(mView.getContext(), infoInput, output.getLogoPath());
    }

    /**
     * 实名认证
     */
    private void openCertificationActivity() {
        IMainProvider provider = PublicProviderUtil.getProvider(IMainProvider.class);
        if (provider != null) {
            provider.openCertificationActivity(mView.getContext(), true);
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
